package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);

        return "/role/list";
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        model.addAttribute("roles",roleService.selectAll());
        if (id!=null){
            Role role = roleService.get(id);
            List<Permission>permission=roleService.selectByRoleId(id);
            model.addAttribute("permission",permission);
            model.addAttribute("role",role);
        }
        return "role/input";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[]ids){
        if (role.getId()==null){
            roleService.save(role,ids);
        }else {
            roleService.update(role,ids);
        }
        return "redirect:/role/list";
    }


    @RequestMapping("/delete")
    public String delete(Long id){
            roleService.delete(id);
            return "redirect:/role/list";
    }


}
